<?php

/**
 * assignment actions.
 *
 * @package    sf_sandbox
 * @subpackage assignment
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class assignmentActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {

        if ($this->getUser()->hasAttribute('teacher_mode')) {
            $this->redirect('home/teacher');
        }

        // ------
        if (!$this->getUser()->hasAttribute('kts_global_program_filter')) {

            $defaults = AcademicYearTable::getInstance()->getDefaults();

            $data['year'] = $defaults['id'];
            $data['semester'] = $defaults['semseter_number'];

            $this->getUser()->setAttribute('kts_global_program_filter', $data);
        }
        // ------
        //$this->forward('default', 'module');
        $manager_teacher = $this->getUser()->getTeacherObject();
        $this->forward404Unless($manager_teacher);
        $school = $manager_teacher->getSchool();
        $this->school_intg_id = $school->getIntgId();
    }

    public function executeAjaxAssign(sfWebRequest $request) {
        $this->program_id = $program_id = $this->getRequestParameter('program_id');
        $teacher_to_assign = $this->getRequestParameter('teacher_to_assign');
        $old_assigned_teacher = $this->getRequestParameter('old_assigned_teacher');
        $school_intg_id = $this->getRequestParameter('school_intg_id');
        $this->assign_index = $this->getRequestParameter('assign_index');

        $program = ProgramTable::getInstance()->find($program_id);
        $this->forward404Unless($program);

        $manager_teacher = $this->getUser()->getTeacherObject();
        $school = $manager_teacher->getSchool();
        $this->my_teachers = $school->getTeachers2(false);

        if (!$teacher_to_assign) {
            $this->message = I18nUtilities::__('Missing Teacher to assign');
            $this->succ = false;
            return;
        } else {
            $teacher = TeacherTable::getInstance()->find($teacher_to_assign);
            if (!$teacher) {
                $this->message = I18nUtilities::__('Missing Teacher to assign');
                $this->succ = false;
            }
        }
        $q = LinkTeacherProgramTable::getInstance()->createQuery('l');
        $q->addWhere('l.program_id = ?', $program_id);
        $q->addWhere('l.teacher_id = ?', $old_assigned_teacher);
        $q->innerJoin('l.Teacher t')
                ->addWhere('t.school_intg_id = ?', $school_intg_id);
        $assign_record = $q->fetchOne();
        if (!$assign_record) {
            $assign_record = new LinkTeacherProgram();
            $assign_record->setProgramId($program_id);
        }
        //$assign_record = $assign_record->toArray();
        if ($assign_record['assign_status'] == LinkTeacherProgramTable::STATUS_APPROVED) {
            $this->message = I18nUtilities::__('Approved assignment cannot be edited');
            $this->succ = false;
        } else {
            $assign_record->assignToTeacher($teacher_to_assign);

            $this->message = I18nUtilities::__('Assigned successfully');
            $this->succ = true;
        }
        $this->new_assign_record = $assign_record;
    }

    public function executeDownloadApproval(sfWebRequest $request) {
        $file = (sfConfig::get('sf_root_dir') . '/rtf/approval1.rtf');

        $file = file_get_contents($file);

        $pid = $request->getParameter('program_id');
        $tid = $request->getParameter('teacher_id');

        $program = ProgramTable::getInstance()->find($pid);
        $teacher = TeacherTable::getInstance()->find($tid);

        Doctrine_Query::create()->update('LinkTeacherProgram')->set('assign_status', '?', 2)->where('program_id = ?', $pid)->andWhere('teacher_id = ?', $tid)->execute();

        $file = str_replace('%program_name%', $this->get_rtf_chars($program->getName()), $file);
        $file = str_replace('%program_duration%', $this->get_rtf_chars($program->getDurationDays()), $file);
        $file = str_replace('%program_location%', $this->get_rtf_chars($program->getLocationsInSector()->getExtendedString()), $file);
        $file = str_replace('%teacher_name%', $this->get_rtf_chars($teacher->getFullName()), $file);
        $file = str_replace('%school_manager%', $this->get_rtf_chars($this->getUser()->getGuardUser()->getTeacher()->getFullName()), $file);
        $file = str_replace('%school_name%', $this->get_rtf_chars($this->getUser()->getGuardUser()->getTeacher()->getSchool()->getName()), $file);
        $file = str_replace('%program_start_date%', $this->get_rtf_chars($program->getStartDate(), 'ltr'), $file);
        $file = str_replace('%program_start_time%', $this->get_rtf_chars($program->getDurationHours(), 'ltr'), $file);
        $file = str_replace('%program_trainer%', $this->get_rtf_chars($program->getTrainer()->__toString()), $file);

        $time = md5(time());

        ob_end_clean();

        header('Content-type: application/rtf');
        header('Content-Disposition: attachment; filename="approval_' . substr($time, strlen($time) - 6, strlen($time)) . '.rtf"');

        die($file);
    }

    private function get_rtf_chars($string, $direction = 'rtl') {

        $len = mb_strlen($string);
        $ret = "\\{$direction}ch\\f0 ";
        $string = iconv('UTF-8', 'CP1256//TRANSLIT', $string);
        for ($i = 0; $i <= $len; $i++) {
            $ord = ord($string{$i});
            if ($ord)
                $ret.= '\\\'' . dechex($ord);
        }

        return $ret;
    }

}
